//
//  FFTradeSystemNotifyCell.m
//  LLWallet
//
//  Created by tt on 2020/9/23.
//

#import "FFTradeSystemNotifyCell.h"
#import "FFTradeSystemNotifyContent.h"
@interface FFTradeSystemNotifyCell ()

@property(nonatomic,strong) UIView *boxView;

@property(nonatomic,strong) UILabel *leftTitleLbl;
@property(nonatomic,strong) UILabel *leftSubTitleLbl;

@property(nonatomic,strong) UILabel *centerTitleLbl;
@property(nonatomic,strong) UILabel *centerSubTitleLbl;

@property(nonatomic,strong) UIView *attrBoxView; // 属性box view

@property(nonatomic,strong) UIView *bottomBoxView; // 底部view
@property(nonatomic,strong) UILabel *urlTitleLbl;
@property(nonatomic,strong) UIImageView *arrowImgView;

@property(nonatomic,strong) UIView *lineView; // lineView

@end

@implementation FFTradeSystemNotifyCell

#define boxBottom 10.0f

#define leftTitleTop 15.0f
#define leftTitleHeight 15.0f

#define leftSubTitleHeight 12.0f
#define leftSubTitleTop 10.0f

#define centerSubTitleHeight 14.0f
#define centerSubTitleTop 40.0f

#define centerTitleHeight 30.0f
#define centerTitleTop 4.0f

#define attrBoxViewTop 20.0f
#define attrItemHeight 30.0f

#define bottomBoxHeight 50.0f
#define bottomBoxTop 20.0f

#define lineHeight 1.0f

+ (CGSize)sizeForMessage:(WKMessageModel *)model {
    FFTradeSystemNotifyContent *content = (FFTradeSystemNotifyContent*)model.content;
    
    return CGSizeMake(WKScreenWidth, leftTitleTop + leftTitleHeight + leftSubTitleHeight+leftSubTitleTop + centerSubTitleHeight + centerSubTitleTop +centerTitleHeight+centerTitleTop+attrBoxViewTop + attrItemHeight*(content.attrs?content.attrs.count:0) + boxBottom + bottomBoxHeight + bottomBoxTop + lineHeight);
}

- (void)initUI {
    [super initUI];
    [self setBackgroundColor:[UIColor clearColor]];
    [self addSubview:self.boxView];
    [self.boxView addSubview:self.leftTitleLbl];
    [self.boxView addSubview:self.leftSubTitleLbl];
    
    [self.boxView addSubview:self.centerTitleLbl];
    [self.boxView addSubview:self.centerSubTitleLbl];
    
    [self.boxView addSubview:self.attrBoxView];
    
    [self.boxView addSubview:self.lineView];
    
    [self.boxView addSubview:self.bottomBoxView];
    
    [self.bottomBoxView addSubview:self.urlTitleLbl];
    [self.bottomBoxView addSubview:self.arrowImgView];
    
}

- (void)refresh:(WKMessageModel *)model {
    [super refresh:model];
    
    FFTradeSystemNotifyContent *content = (FFTradeSystemNotifyContent*)model.content;
    self.leftTitleLbl.text = content.leftTitle;
    self.leftSubTitleLbl.text = content.leftSubtitle;
    
    self.centerTitleLbl.text = content.centerTitle;
    self.centerSubTitleLbl.text = content.centerSubtitle;
    
    self.urlTitleLbl.text = content.urlTitle;
    
    [[self.attrBoxView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if(content.attrs && content.attrs.count>0) {
        for (NSString *key in content.attrs.allKeys) {
            FFTradeAttrItemView *itemView = [[FFTradeAttrItemView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, attrItemHeight)];
            itemView.titleLbl.text = key;
            itemView.valueLbl.text = content.attrs[key];
            [self.attrBoxView addSubview:itemView];
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // box
    self.boxView.lim_width = WKScreenWidth - 40.0f;
    self.boxView.lim_height = self.lim_height - boxBottom;
    self.boxView.lim_left = self.lim_width/2.0f - self.boxView.lim_width/2.0f;
    
    // leftTitle
    [self.leftTitleLbl sizeToFit];
    self.leftTitleLbl.lim_left = 15.0f;
    self.leftTitleLbl.lim_height = leftTitleHeight;
    self.leftTitleLbl.lim_top = leftTitleTop;
    
    // leftSubTitle
    [self.leftSubTitleLbl sizeToFit];
    self.leftSubTitleLbl.lim_height = leftSubTitleHeight;
    self.leftSubTitleLbl.lim_top = self.leftTitleLbl.lim_bottom + leftSubTitleTop;
    self.leftSubTitleLbl.lim_left = self.leftTitleLbl.lim_left;
    
    // centerSubTitle
    [self.centerSubTitleLbl sizeToFit];
    self.centerSubTitleLbl.lim_height = centerSubTitleHeight;
    self.centerSubTitleLbl.lim_top = self.leftSubTitleLbl.lim_bottom + centerSubTitleTop;
    self.centerSubTitleLbl.lim_left = self.boxView.lim_width/2.0f - self.centerSubTitleLbl.lim_width/2.0f;
    
    // centerTitle
    [self.centerTitleLbl sizeToFit];
    self.centerTitleLbl.lim_height = centerTitleHeight;
    self.centerTitleLbl.lim_top = self.centerSubTitleLbl.lim_bottom + centerTitleTop;
    self.centerTitleLbl.lim_left = self.boxView.lim_width/2.0f - self.centerTitleLbl.lim_width/2.0f;
    
    // attrs
    self.attrBoxView.lim_width = self.boxView.lim_width;
    self.attrBoxView.lim_top = self.centerTitleLbl.lim_bottom + attrBoxViewTop;
    if(self.attrBoxView.subviews.count>0) {
        NSInteger i =0;
        for (UIView *view in self.attrBoxView.subviews) {
            view.lim_width = self.boxView.lim_width;
            view.lim_top = i*attrItemHeight;
            i++;
        }
        self.attrBoxView.lim_height = i*attrItemHeight;
    }
    
    // line
    self.lineView.lim_top = self.attrBoxView.lim_bottom + bottomBoxTop;
    self.lineView.lim_width = self.boxView.lim_width - 30.0f;
    self.lineView.lim_left = 15.0f;

    // bottomBox
    self.bottomBoxView.lim_height = bottomBoxHeight;
    self.bottomBoxView.lim_width = self.boxView.lim_width;
    self.bottomBoxView.lim_top = self.lineView.lim_bottom;
    
   

    // urlTitle
    [self.urlTitleLbl sizeToFit];
    self.urlTitleLbl.lim_top = self.bottomBoxView.lim_height/2.0f - self.urlTitleLbl.lim_height/2.0f;
    self.urlTitleLbl.lim_left = 15.0f;

    // arrow
    self.arrowImgView.lim_top = self.bottomBoxView.lim_height/2.0f - self.arrowImgView.lim_height/2.0f;
    self.arrowImgView.lim_left = self.bottomBoxView.lim_width - self.arrowImgView.lim_width - 15.0f;
    
    
}


- (UIView *)boxView {
    if(!_boxView) {
        _boxView = [[UIView alloc] init];
        [_boxView setBackgroundColor:[UIColor whiteColor]];
        _boxView.layer.cornerRadius = 4.0f;
    }
    return _boxView;
}

- (UILabel *)leftTitleLbl {
    if(!_leftTitleLbl) {
        _leftTitleLbl = [[UILabel alloc] init];
        _leftTitleLbl.font = [[WKApp shared].config appFontOfSize:16.0f];
    }
    return _leftTitleLbl;
}
- (UILabel *)leftSubTitleLbl {
    if(!_leftSubTitleLbl) {
        _leftSubTitleLbl = [[UILabel alloc] init];
        _leftSubTitleLbl.font = [[WKApp shared].config appFontOfSize:12.0f];
        _leftSubTitleLbl.textColor = [WKApp shared].config.tipColor;
    }
    return _leftSubTitleLbl;
}

- (UILabel *)centerTitleLbl {
    if(!_centerTitleLbl) {
        _centerTitleLbl = [[UILabel alloc] init];
        _centerTitleLbl.font = [[WKApp shared].config appFontOfSizeMedium:30.0f];
    }
    return _centerTitleLbl;
}

- (UILabel *)centerSubTitleLbl {
    if(!_centerSubTitleLbl) {
        _centerSubTitleLbl = [[UILabel alloc] init];
        _centerSubTitleLbl.font = [[WKApp shared].config appFontOfSize:14.0f];
        _centerSubTitleLbl.textColor = [WKApp shared].config.tipColor;
    }
    return _centerSubTitleLbl;
}

- (UIView *)attrBoxView {
    if(!_attrBoxView) {
        _attrBoxView = [[UIView alloc] init];
    }
    return _attrBoxView;
}

- (UIView *)bottomBoxView {
    if(!_bottomBoxView) {
        _bottomBoxView = [[UIView alloc] init];
    }
    return _bottomBoxView;
}

- (UIImageView *)arrowImgView {
    if(!_arrowImgView) {
        _arrowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 22.0f, 22.0f)];
        [_arrowImgView setImage:[self imageName:@"arrow_right"]];
        
    }
    return _arrowImgView;
}

- (UILabel *)urlTitleLbl {
    if(!_urlTitleLbl) {
        _urlTitleLbl = [[UILabel alloc] init];
        _urlTitleLbl.font = [[WKApp shared].config appFontOfSize:16.0f];
    }
    return _urlTitleLbl;
}

- (UIView *)lineView {
    if(!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, lineHeight)];
        _lineView.backgroundColor = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0f];
    }
    return _lineView;
}

-(UIImage*) imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LLWallet"];
}
@end



@implementation FFTradeAttrItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.titleLbl];
        [self addSubview:self.valueLbl];
    }
    return self;
}

- (UILabel *)titleLbl {
    if(!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.font = [[WKApp shared].config appFontOfSize:16.0f];
        _titleLbl.textColor = [WKApp shared].config.tipColor;
    }
    return _titleLbl;
}

- (UILabel *)valueLbl {
    if(!_valueLbl) {
        _valueLbl = [[UILabel alloc] init];
        _valueLbl.font = [[WKApp shared].config appFontOfSize:16.0f];
        _valueLbl.textColor = [WKApp shared].config.defaultTextColor;
    }
    return _valueLbl;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.titleLbl sizeToFit];
    self.titleLbl.lim_left = 15.0f;
    self.titleLbl.lim_top = self.lim_height/2.0f - self.titleLbl.lim_height/2.0f;
    
    [self.valueLbl sizeToFit];
    self.valueLbl.lim_left = 100.0f;
    self.valueLbl.lim_top = self.lim_height/2.0f - self.valueLbl.lim_height/2.0f;
    
}


@end
